/**
 * 
 */
package com.ao.andou.common;

import java.util.HashMap;

import android.net.Uri;
import android.text.TextUtils;

/**
 * String Helper
 * @author pentiumao
 *
 */
public final class StringUtlis {

    /* params splitter */
    private final static char AND_CHAR = '&';
    
    /* question char */
    private final static char QUESTION_CHAR = '?';
    
    /* equal char */
    private final static char EQUAL_CHAR = '=';
    
    /**
     * Get request url with parameters. Just put parameters into the map.
     * 
     * @param url target url
     * @param params params key and value
     * @return the request address
     */
    public static String getUrlWithParams(String url, HashMap<String, String> params){
        
        if(TextUtils.isEmpty(url)){
            throw new IllegalArgumentException("url is empty");
        }
        
        StringBuilder paramsString = new StringBuilder(url);
        if(params != null && params.size() > 0){
            paramsString.append(QUESTION_CHAR);
            for(String key : params.keySet()){
                // encode url parameters
                String value = Uri.encode(params.get(key));
                // key=value
                paramsString.append(key).append(EQUAL_CHAR).append(value).append(AND_CHAR);
            }
            // remove the last '&'
            paramsString.setLength(paramsString.length() - 1);
        }
        return paramsString.toString();
    }
}
